// /**
//  * 
//  * @authors cherish yii2 (cherish@cherish.pw)
//  * @date    2020-12-10 16:48:28
//  * @version v1.0
//  * @description the core js of todolist project
//  * 
//  * ━━━━━━神兽出没━━━━━━
//  * 　　   ┏┓　 ┏┓
//  * 　┏━━━━┛┻━━━┛┻━━━┓
//  * 　┃              ┃
//  * 　┃       ━　    ┃
//  * 　┃　  ┳┛ 　┗┳   ┃
//  * 　┃              ┃
//  * 　┃       ┻　    ┃
//  * 　┃              ┃
//  * 　┗━━━┓      ┏━━━┛ Code is far away from bugs with the animal protecting.
//  *       ┃      ┃     神兽保佑,代码无bug。
//  *       ┃      ┃
//  *       ┃      ┗━━━┓
//  *       ┃      　　┣┓
//  *       ┃      　　┏┛
//  *       ┗━┓┓┏━━┳┓┏━┛
//  *     　  ┃┫┫　┃┫┫
//  *     　  ┗┻┛　┗┻┛
//  *
//  * ━━━━━━感觉萌萌哒━━━━━━
//  */

// // 请根据考试说明文档中列出的需求进行作答
// // 预祝各位顺利通过本次考试，see you next week！
// // ...


function load(){
    var todo = document.getElementById("todolist"),
     done = document.getElementById("donelist"),
     todocount = document.getElementById("todocount"),
     donecount = document.getElementById("donecount"),
     todoString = "",
     doneString = "",
     todoCount = 0,
     doneCount = 0;
    document.getElementById("todolist").focus();
    todolist = loadData();
    if (todolist != null){
     for (var i=0; i<todolist.length; i ++){
      if(!todolist[i].done){
       todoString += "<li>"
        + "<input type='checkbox' onchange='update("+i+", \"done\", true)'>"
        + "<p id='p-"+i+"' onclick='edit("+i+")'>" + todolist[i].todo + "</p>" +
        "<a onclick='remove("+i+")'>-</a>" +
        "</li>";
       todoCount ++;
      }
      else{
       doneString += "<li>"
        + "<input type='checkbox' "
        + "onchange='update("+i+", \"done\", false)' checked>"
        + "<p id='p-"+i+"' onclick='edit("+i+")'>" + todolist[i].todo + "</p>"
        + "<a onclick='remove("+i+")'>-</a>"
        + "</li>";
       doneCount ++;
      }
     }
     todo.innerHTML = todoString;
     done.innerHTML = doneString;
     todocount.innerHTML = todoCount;
     donecount.innerHTML = doneCount;
    }
    else {
     todo.innerHTML = "";
     done.innerHTML = "";
     todocount.innerHTML = 0;
     donecount.innerHTML = 0;
    }
   }

//实现“添加代办事项”功能 
function addTodoList(e) {
    var obj_list = {
        todo: "",
        done: false
    }
document.getElementById("todolist").value = document.getElementById("todolist").value.trim();
    if (document.getElementById("todolist").value.length === 0) {
    alert("不能为空");
    return;
}
obj_list.todo = document.getElementById("todolist").value;
todolist.push(obj_list);
saveData(todolist);
document.getElementById("todolist").value = "";
    load();
document.getElementById("todolist").focus();
}


//实现“正在进行”部分的相关需求

function edit(i){
    var p = document.getElementById('p-' + i);
    pContent = p.innerHTML,
    inputId;
}
function confirm(){
    if (inputId.value.length === 0){
        p.innerHTML = pContent;
        alert("内容不能为空");
    }else{
        update(i,"todo",inputId.value);
    }
}
function enter(e){
    if (e.keycode = 13) {
        confirm();
    }
    p.innerHTML = "<input type='text' id='input -"+i+"' value = '"+ pContent +"' >";
    inputId = document.getElementById("input-" + i);
    inputId.focus();
    inputId.setSelectionRange(0,input.value.length);
    inputId.onblur = confirm;
    inputId.onkeypress = enter; 
}

function update(i,field,value){
    todolist[i][field]=value;
    saveData(todolist);
    load();
}

function remove(obj){
    if (w) {
        var todolist = obj.id;
        var o = document.getElementById(todolist);
        var z = o.parentElement;
        var todolist = z.id;

        var my = document.getElementById(todolist);
        if (my != null) {
            my.parentNode.removeChild(my);
        }
    }
}

function remove(i){
    todolist.splice(i,1);
    saveData(todolist);
    load();
}


function saveData(data){
    localStorage.setItem("mytodolist",JSON.stringify(data));
}


function loadData(){
    var hisTory = localStorage.getItem("mytodolist");
    if (hisTory != null) {
        return JSON.parse(hisTory);
    }else{
        return[];
    }
}


function clear(){
    localStorage.clear();
    load();
}

// window.addEventListener("load",load);
// document.getElementById("clearbutton").onclick = clear;
// document.getElementById("add_list").onkeypress = function(event){
//     if (event.keycode===13) {
//         addTodoList();
//     }
// }

